package com.teapos.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teapos.service.DailyService;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/daily")
@Controller
public class DailyController extends BaseController {
	
	@Autowired
	private DailyService dailyService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest req) {
		req.setAttribute("store_name", super.getUser(req).get("store_name"));
		
		
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		req.setAttribute("logs", dailyService.findDailyLogs(param));
		req.setAttribute("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				dailyService.getTotalCount(param), "js", "turnpage"));
		return "daily/index";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public String save(HttpServletRequest req) {
		try {
			Map reqMap = super.getParaMap(req);
			reqMap.put("user", super.getUser(req));
			dailyService.saveExpense(reqMap);
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@RequestMapping("/list")
	public String list(HttpServletRequest req) {
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		req.setAttribute("logs", dailyService.findDailyLogs(param));
		req.setAttribute("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				dailyService.getTotalCount(param), "js", "turnpage"));
		return "daily/list";
	}

}
